
//let baseUrl ='https://dyr-test.api.ahjgkj.com'           //测试域名
let baseUrl =wx.getStorageSync('currentEnv')=='dev'?'https://dyr-test.api.ahjgkj.com':'https://dyr.api.ahjgkj.com'
const loginUrl ='/mini/site/wx-login'                        //登录
const statusUrl ='/mini/site/spoker-info'                    //用户状态
const homeUrl ='/mini/site/index'                            //首页
const getPhoneUrl ="/mini/site/get-wx-phone"                 //获取手机号
const addSpokensmanUrl ="/mini/user/create-spoker"           //新增代言人
const clientUrl ="/mini/patient/list"                        //客户列表
const arriveUrl ="/mini/patient/arrive-list"                 //到店列表
const consumeUrl ="/mini/patient/consume-list"               //消费列表
const createClient ="/mini/patient/create"                   //登记患者
const clientInfo ="/mini/patient/edit-info"                  //患者编辑信息
const clientDetail ="/mini/patient/info"                     //患者信息
const updateClient ="/mini/patient/update"                   //编辑患者
const orderInfo ="/mini/patient/edit-order-info"             //患者编辑预约信息
const addOrder ="/mini/patient/create-order"                 //新增预约
const updateOrder ="/mini/patient/update-order"              //编辑预约
const orderList ="/mini/patient/order-list"                  //预约列表
const mineInfo ="/mini/user/info"                            //我的
const statisticsUrl ="/mini/user/statistics-data"            //我的【统计数据】
const feedbackList ="/mini/user/feedback-list"               //客户意见列表
const feedbackInfo ="/mini/user/feedback-info"               //意见信息
const createFeedback ="/mini/user/create-feedback"           //提交意见
const withdrawInfo ="/mini/user/withdraw-info"               //提现信息
const editWithdraw ="/mini/user/edit-withdraw"               //编辑提现账号
const consumeRank ="/mini/rank/consume-list"                 //消费排行列表
const arriveRank ="/mini/rank/arrive-list"                   //到店排行列表
const patientRank ="/mini/rank/patient-list"                 //新增排行列表


function getToken(data, type) {
  return request(loginUrl, data, type)
}

function request (url,data,type='get') {
  let token = wx.getStorageSync('userInfo') ? '?auth_access_token=' + wx.getStorageSync('token') : ''
  
  return new Promise(function (resolve, reject) {
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.request({
      url: baseUrl + url + token,
      data: data,
      method: type,
      header: {
        'content-type': 'application/json', // 默认值
        'Cache-Control': 'max-age=180'
      },
      success: function (res) {
        if(res.data.code==200){
          resolve(res.data)  
          wx.hideLoading()        
        } else if (res.data.code == 401){      
          // 将后端返回的token存储到本地
          wx.setStorageSync('token', '')
          wx.setStorageSync('userInfo', '')
          wx.hideLoading()
          getLogin()
          return;
        }else{
          wx.hideLoading()
          if(res.data.code!=500){
            wx.showToast({
              title: res.data.message,
              icon: 'error',
              duration: 3000
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'error',
              duration: 3000
            })
          }
          return;
        }
      },
      fail: function (err) {
        //服务器连接异常
        reject(err, "服务器连接异常，请检查网络再试")
        
      },
    })
  });
}
export { baseUrl }
export function envChange(env){
  wx.setStorageSync('currentEnv',env)
  wx.setStorageSync('tel','')
  baseUrl =wx.getStorageSync('currentEnv')=='dev'?'https://dyr-test.api.ahjgkj.com':'https://dyr.api.ahjgkj.com'
  login()
}
export function updateWithdraw(data, type='post') {
  return request(editWithdraw, data, type)
}
export function getWithdrawInfo(data, type) {
  return request(withdrawInfo, data, type)
}
export function addFeedback(data, type='post') {
  return request(createFeedback, data, type)
}
export function getFeedbackInfo(data, type) {
  return request(feedbackInfo, data, type)
}
export function getFeedbackList(data, type) {
  return request(feedbackList, data, type)
}
export function getInfo(data, type) {
  return request(mineInfo, data, type)
}
export function getStatisticsData(data, type) {
  return request(statisticsUrl, data, type)
}
export function getHomeInfo(data, type) {
  return request(homeUrl, data, type)
}
export function getClientList(name,data, type) {
  let url=clientUrl
  switch(name){
    case 'a':
      url=clientUrl
      break;
    case 'b':
      url=orderList
      break;
    case 'c':
      url=arriveUrl
      break;
    case 'd':
      url=consumeUrl
      break;
  }
  return request(url, data, type)
}
export function getClientInfo(data, type) {
  return request(clientInfo, data, type)
}
export function getClientDetail(data, type) {
  return request(clientDetail, data, type)
}
export function addClient(data, type='post') {
  return request(createClient, data, type)
}
export function getOrderInfo(data, type) {
  return request(orderInfo, data, type)
}
export function editClient(data, type='post') {
  return request(updateClient, data, type)
}
export function createOrder(data, type='post') {
  return request(addOrder, data, type)
}
export function editOrder(data, type='post') {
  return request(updateOrder, data, type)
}
export function getPhone(data, type='post') {
  return request(getPhoneUrl, data, type)
}
export function addSpokensman(data, type='post') {
  return request(addSpokensmanUrl, data, type)
}
export function login() {
  getLogin()
}
export function junpTo() {      
  getStatus().then(res=>{   
    wx.setStorageSync('switch',res.data.switch)  
    if(res.data.id==0){
      skipTo();
      return;
    }
    skipTo(res.data.status);
  })
}
export function getRankList(name,data, type) {
  let url=consumeRank
  switch(name){
    case 'a':
      url=consumeRank
      break;
    case 'b':
      url=arriveRank
      break;
    case 'c':
      url=patientRank
      break;
  }
  return request(url, data, type)
}


function getStatus() {
  return request(statusUrl)
}

function getLogin(){
  wx.login({
    success: res => {
      getToken({code:res.code},'post').then(res=>{
        wx.setStorageSync('token', res.data.access_token)
        wx.setStorageSync('userInfo', res.data.info)       
        junpTo()
      })
    }
  })
}

function skipTo(type){
  switch(type){
    case 0 :
      wx.reLaunch({
        url: '/pages/result/result?type='+type,
      })
      break;
    case 1:
      wx.reLaunch({
        url: '/pages/client/client',
      })
      break;
    case 2:
      wx.reLaunch({
        url: '/pages/result/result?type='+type,
      })
      break;
    default:
      wx.reLaunch({
        url: '/pages/home/home',
      })
      break;
  }
}
function change(t) {
  if (t < 10) {
    return "0" + t;
  } else {
    return t;
  }
}
export function formatDate(a,type) {
  let date=new Date(a)
  let year = date.getFullYear()
  let month = change(date.getMonth() + 1)
  let day = change(date.getDate())
  let hour=change(date.getHours())
  let minute=change(date.getMinutes())
  if(type==1){
    return year + '-' + month + '-' + day;
  }else{
    return year + '-' + month + '-' + day+' '+ hour + ':'+minute;
  }
 
}
export function toTemp(a){
  return new Date(a).getTime();
}
